package models;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import play.db.jpa.Model;

/**
 * @author Omar Eissa
 * This is the model for Notifications which stores the user who is sending
 * and the receiving user receiving. The action which is the string that 
 * the sender wants the receiver to accept or reject. viewed describes 
 * if the user have seen this notification before or not to highlight 
 * the notification itself for a better view to the user 
 */

@Entity
public class Notification extends Model {
	public String action;
	public String sender;
	@ManyToOne
	public User receiver;
	public Date date;
	public boolean viewed;

	public Notification(String action, String sender, User receiver, Date date) {
		this.action = action;
		this.sender = sender;
		this.receiver = receiver;
		this.date = date;
		this.viewed = false;
	}
	
	/**
	 * @author Omar Eissa

	 * @param receiverId: This is the id of a user
	 * @return listOfNotification: A List that returns the notifications for the user
	 * Description: This is a method that takes the receiverId and returns a list 
	 * 				of Notifications of the user whom his id is equivelant to 
	 * 				the receiverId
	 */
	public static List<Notification> retrieveNotificationsOfAUser(long receiverId) {
		List<Notification> listOfNotifications = Notification.find(
				"receiver_id = ?", receiverId).fetch();
		return listOfNotifications;

	}
	public void setViewed() {
		this.viewed = true;
		this.save();
	}
}
